package com.taobao.snowsnow.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;


/**
 * @author tux
 * 延迟加载时要用到了类
 *
 */
public class Lazy {
	private SessionFactory sessionFactory;
	private  Session session;
	
	public Lazy() {
		init();
	}
	
	public void init() {
		sessionFactory = (SessionFactory) ApplicationContextUtil.getBean("sessionFactory");
		session = sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
	}
	
	public void close() {
		TransactionSynchronizationManager.unbindResource(sessionFactory);   
        SessionFactoryUtils.releaseSession(session, sessionFactory);   
	}
}
